﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using CjaxExam.Models.DataBase;
using CjaxExam.Models.Interfaces;
using System.Collections;

namespace CjaxExam.Models.Entities
{
    public class User:IUser
    {
        public IList<ex_user> ListAll()
        { 
            ForExamDataContext abc = new ForExamDataContext();
            var users = from u in  abc.ex_user
                        select u;
            return users.ToList();

        }

        public string GetId(int no)
        {
            string backid = null;
            //no为获取资源ID的数字标识
            ForExamDataContext user = new ForExamDataContext();
            user.ForId(no, ref backid);
            return backid;
        }

        public List<ex_user> CheckLoginname(string loginname)
        {
            ForExamDataContext checkname = new ForExamDataContext();
            var checkloginname = from p in checkname.ex_user
                                 where p.user_username == loginname
                                 select p;
            return checkloginname.ToList();
        }

        public List<dictionary> GetSeconddep(string dep)
        {
            ForExamDataContext k = new ForExamDataContext();
            var seconddep = from p in k.dictionary
                            where p.name == dep
                            select p;
            return seconddep.ToList();
        }

        public void InsertUser(UserVO e)
        {
            ForExamDataContext insert = new ForExamDataContext();
            ForExamDataContext insertrole = new ForExamDataContext();
            var rolemenberid = from p in insert.ex_roles
                             where p.rolename == "普通角色"
                             select p.roleid;
            
            var euser = new ex_user
            {
               user_name = e.User_name,
               user_department=e.User_department,
               user_job=e.User_job,
               user_password=e.User_password,
               user_post=e.User_post,
               user_sepcial=e.User_sepcial,
               user_userid=e.User_userid,
               user_username = e.User_username,
               user_secondpart = e.User_seconddepart
            };
            insert.ex_user.InsertOnSubmit(euser);
            foreach (var id in rolemenberid)
            {
                var erole = new ex_rolemenbers
                {

                    roleid = id,
                    user_userid = e.User_userid
                };
                insertrole.ex_rolemenbers.InsertOnSubmit(erole);
            }
            insert.SubmitChanges();
            insertrole.SubmitChanges();
        }

        public IList<ex_user> GetUserInfo(string id)
        {
            //获取用户的基本信息
            ForExamDataContext user = new ForExamDataContext();
            var guser = from p in user.ex_user
                        where p.user_userid == id
                        select p;
            return guser.ToList();
        }
        public void UpdateUser(string id)
        {
            //ForExamDataContext update = new ForExamDataContext();
            //User guser = new User();
            //List<ex_user> update =new List<ex_user>();
            //id = guser.GetUserInfo(id);

            //foreach(ex_user e in 
            //var q = from p in up.ex_user
            //        where p.user_userid == guser.GetUserInfo(id)
            //        select p;

            
            //var euser = new ex_user
            //{
            //    user_name = e.User_name,
            //    user_department = e.User_department,
            //    user_job = e.User_job,
            //    user_password = e.User_password,
            //    user_post = e.User_post,
            //    user_sepcial = e.User_sepcial,
            //    user_username = e.User_username
            //};
            //upate.ex_user.InsertOnSubmit(euser);
            //upate.SubmitChanges();
        }


        public IList<ex_roles> ListAllRole()
        {
            ForExamDataContext abc = new ForExamDataContext();
            var users = from u in abc.ex_roles
                        select u;
            return users.ToList();

        }
        public IList<ex_roles> GetRoleInfo(string id)
        {
            //获取用户的基本信息
            ForExamDataContext role = new ForExamDataContext();
            var grole = from p in role.ex_roles
                        where p.roleid == id
                        select p;
            return grole.ToList();
        }
        public void InsertRole(RoleVO e)
        {
            ForExamDataContext insert = new ForExamDataContext();
            var erole = new ex_roles
            {
                roleid=e.Roleid,
                rolename=e.Rolename,
                power=int.Parse(e.Power)
            };
            insert.ex_roles.InsertOnSubmit(erole);
            insert.SubmitChanges();
        }
    }
}
